/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdh;

/**
 * This class contains the information to connect the server.
 * @author tale
 */
public class ServerConfig {

    public enum engineType {
        MYSQL,
        ORACLE,
        SQLSERVER,
        //DM,   // not support
    }

    engineType type;

    public engineType getType(){
        return type;
    }

    public boolean setType(engineType type){
        this.type = type;
        return true;
    }

    protected String server;

    public boolean setServer(String server){
        this.server = server;
        return true;
    }

    public String getServer(){
        return server;
    }
    protected String username;

    public boolean setUsername(String username){
        if (username.compareTo("") == 0)
            return false;
        this.username = username;
        return true;
    }

    public String getUsername(){
        return username;
    }

    protected String password;

    public boolean setPassword(String password){
        this.password = password;
        return true;
    }

    //FIXME: should i return the password to the public??
    public String getPassword(){
        return password;
    }

    protected String database;

    public boolean setDatabase(String database){
        if (database.matches("\\w+")){   //match a WORD
            this.database = database;
            return true;
        }
        return false;
    }

    public String getDatabase(){
        return database;
    }

    public String getUrl(){
        switch(type){
            default:
            case MYSQL:
                return "jdbc:mysql://"+server +"/" + database
                        +"?useUnicode=true&characterEncoding=utf8";
            case ORACLE:
                return "";
            case SQLSERVER:
                return "jdbc:sqlserver://" + server + "; DatabaseName=" + database;
        }
    }

    public String getDriver(){
        switch(type){
            default:
            case MYSQL:
                return "com.mysql.jdbc.Driver";
            case ORACLE:
                return "oracl.jdbc.driver.OracleDriver";
            case SQLSERVER:
                return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        }
    }

    // Let's provide some default values for convenience.
    public void setDefault(){
        if (System.getProperty("os.name").toLowerCase().indexOf("linux") >=0 ){
            type    = engineType.MYSQL;
            username = "root";
        }
        else{
            type    = engineType.SQLSERVER;
            username = "sa";
        }
        server  = "localhost";
        password = "winter";
        database = "HOMEWORK";
    }

    public ServerConfig(){
        setDefault();
    }

    public ServerConfig(String server){
        setDefault();
        this.server = server;
    }

    public ServerConfig(String server, String database){
        setDefault();
        this.server = server;
        this.database = database;
    }

    public static void main(String[] args){
        System.getProperties().list(System.out);
    }
}
